package com.ruoyi.system.service.vantservice.customerervice.impl;

import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.domain.customer.PayableInstalment;
import com.ruoyi.system.mapper.vant.customer.ApiPayableInstalmentMapper;
import com.ruoyi.system.service.vantservice.customerervice.ApiPayableInstalmentService;
import com.ruoyi.system.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class ApiPayableInstalmentServiceImpl implements ApiPayableInstalmentService {

    @Autowired
    private ApiPayableInstalmentMapper payableInstalmentMapper;

    @Autowired
    private TokenUtils tokenUtils;
    @Autowired
    private HttpServletRequest request;

    @Override
    public PayableInstalment selectPayableInstalmentById(Long instalmentId) {
        return payableInstalmentMapper.selectPayableInstalmentById(instalmentId);
    }

    @Override
    public PayableInstalment selectinstalmentState(String instalmentState) {
        return payableInstalmentMapper.selectinstalmentState(instalmentState);
    }

    @Override
    public List<PayableInstalment> selectPayableInstalmentList(PayableInstalment payableInstalment) {
        return payableInstalmentMapper.selectPayableInstalmentList(payableInstalment);
    }

    @Override
    public List<PayableInstalment> selectPayableInstalment(PayableInstalment payableInstalment) {
        return payableInstalmentMapper.selectPayableInstalment(payableInstalment);
    }

    @Override
    public List<PayableInstalment> selectPayableInstalmentVoucherTowList(PayableInstalment payableInstalment) {
        return payableInstalmentMapper.selectPayableInstalmentVoucherTowList(payableInstalment);
    }

    @Override
    public List<PayableInstalment> selectPayableInstalmentEndAuditList(PayableInstalment payableInstalment) {
        return payableInstalmentMapper.selectPayableInstalmentEndAuditList(payableInstalment);
    }

    @Override
    public int insertPayableInstalment(PayableInstalment payableInstalment) {
        Long numconstructionId = payableInstalment.getNumconstructionId();
        payableInstalment.setConstructionId(numconstructionId);

        Double numpice = payableInstalment.getNumpice();
        payableInstalment.setInstalmentPice(numpice);
        payableInstalment.setModifiedTime(new Date());

        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        payableInstalment.setCustomerId(customerLogin.getCustomerId());

        String numinstalmentState = payableInstalment.getNuminstalmentState();
        payableInstalment.setInstalmentState(numinstalmentState);
        return payableInstalmentMapper.insertPayableInstalment(payableInstalment);
    }

    @Override
    public int updatePayableInstalment(PayableInstalment payableInstalment) {
        String firstList = payableInstalment.getFirstList();
        System.err.println("*********************00000000000000***************************************"+firstList);
        payableInstalment.setFirstList(firstList);

        String lastList = payableInstalment.getLastList();
        System.err.println("*********************000000lastList00000000***************************************"+lastList);

        payableInstalment.setLastList(lastList);

        String numtState = payableInstalment.getNumtState();
        payableInstalment.setInstalmentState(numtState);

        Integer numoneMoney = payableInstalment.getNumoneMoney();
        payableInstalment.setOneMoney(numoneMoney);
        Integer numtwoMoney = payableInstalment.getNumtwoMoney();
        payableInstalment.setTwoMoney(numtwoMoney);
        Integer numthreeMoney = payableInstalment.getNumthreeMoney();
        System.err.println("********numthreeMoney***********"+numthreeMoney);
        payableInstalment.setThreeMoney(numthreeMoney);
        Integer numfourMoney = payableInstalment.getNumfourMoney();
        payableInstalment.setFourMoney(numfourMoney);
        Integer numfiveMoney = payableInstalment.getNumfiveMoney();
        payableInstalment.setFiveMoney(numfiveMoney);

        Integer numfirstPayment = payableInstalment.getNumfirstPayment();
        System.err.println("*******=numfirstPayment=*********"+numfirstPayment);
        payableInstalment.setFirstPayment(numfirstPayment);
        Integer numlastPayment = payableInstalment.getNumlastPayment();
        System.err.println("********+numlastPayment+******"+numlastPayment);
        payableInstalment.setLastPayment(numlastPayment);

        String zhuangtai = payableInstalment.getZhuangtai();
        payableInstalment.setZhuangtai(zhuangtai);

        Long instalmentId = payableInstalment.getInstalmentId();
        payableInstalment.setInstalmentId(instalmentId);
        return payableInstalmentMapper.updatePayableInstalment(payableInstalment);
    }

    @Override
    public String updateTai(PayableInstalment payableInstalment) {
        Integer numlastPayment = payableInstalment.getNumlastPayment();
        System.err.println("********+numlastPayment+******"+numlastPayment);
        payableInstalment.setLastPayment(numlastPayment);

        String numtState = payableInstalment.getNumtState();
        payableInstalment.setInstalmentState(numtState);
        return payableInstalmentMapper.updateTai(payableInstalment);
    }

    @Override
    public int deletePayableInstalmentById(Long instalmentId) {
        return payableInstalmentMapper.deletePayableInstalmentById(instalmentId);
    }

    @Override
    public int deletePayableInstalmentByIds(Long[] instalmentIds) {
        return payableInstalmentMapper.deletePayableInstalmentByIds(instalmentIds);
    }

    @Override
    public List<PayableInstalment> findAllListById(PayableInstalment payableInstalment) {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        System.err.println("******************************************"+customerLogin.getCustomerId());
        payableInstalment.setCustomerId(customerLogin.getCustomerId());
        return payableInstalmentMapper.findAllListById(payableInstalment);
    }
}
